/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.wzm.moviepic.fragment;

import java.util.ArrayList;




import net.tsz.afinal.FinalHttp;


import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.wzm.bean.AdInfo;
import com.wzm.bean.PicInfo;
import com.wzm.manager.Tag;
import com.wzm.moviepic.R;
import com.wzm.moviepic.weight.HackyViewPager;
import com.wzm.moviepic.weight.ScrollTextView;
import com.wzm.utils.Logger;

/**
 * 电影图片展示页面
 * @author zhengzhiyou
 */

public class AdViewPagerActivity extends Activity{

	private ViewPager mViewPager;
	private AdSamplePagerAdapter spAdapter;
	
	private ArrayList<PicInfo> pList = new ArrayList<PicInfo>();
	private FinalHttp fh = null;
	//private static FinalBitmap fb = null;
	private Context mContext;
	//private String movieid = "-1";
	private AdInfo adinfo = null;
	
	//显示图片页码
	private TextView tv_pageshow;
	
	
	
	
	private LinearLayout lly_head;
	private Button btn_back;
	private TextView tv_moviename;
	
	
	
	//行数
	private TextView tv_numline;
	private ScrollTextView tv_mmIntro;	
	private TextView mMovieIntro;
	private int numlines = 0;
	private int curLine = 0;

	
	private Button btn_readall = null;
	
	private int curPage = 0;

	
	private LinearLayout lly_botmintro;
	private LinearLayout lly_bottom;

	
	
	
	private SeekBar mSeekBar;
	private TextView mSeekShow;
	
	
	
	//底部渐现
	private Animation animation = null;
	//底部渐隐
	private Animation animation1 = null;
	//头部出现
	private Animation head_in = null;
	
	//头部隐藏
	private Animation head_out = null;
	
	//广告
	private LinearLayout lly_left = null;
	private LinearLayout lly_right = null;
	
	private int curPos = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		
		adinfo = (AdInfo)getIntent().getSerializableExtra("adinfo");
		if(adinfo==null)
		{
			Toast.makeText(mContext, "参数错误", Toast.LENGTH_SHORT).show();
			finish();
			return;
		}
		
		
		//Logger.info("size:"+adinfo.getAdvpic().size());
		pList = adinfo.getAdvpic();
		//pList.add(null);
		//mViewPager = new HackyViewPager(this);
		setContentView(R.layout.activity_viewpager);
		mViewPager = (HackyViewPager)findViewById(R.id.viewpager);
		
		tv_pageshow = (TextView)findViewById(R.id.pageshow);
		
		animation = AnimationUtils.loadAnimation(this, R.anim.fadeout);
		animation1 = AnimationUtils.loadAnimation(this, R.anim.fadein);
		head_in  = AnimationUtils.loadAnimation(this, R.anim.headerin);
		head_out = AnimationUtils.loadAnimation(this, R.anim.headerout);
		
		
		lly_head = (LinearLayout)findViewById(R.id.movie_head);
		lly_bottom = (LinearLayout)findViewById(R.id.movieinfo_bottom_bar);
		lly_botmintro = (LinearLayout)findViewById(R.id.lly_botmintro_bar);
		
		//请求广告////////////////////////////////////////////////////////
//		lly_center = (LinearLayout)findViewById(R.id.lly_ad);
//		AdsMogoLayout adsMogoLayoutCode = new AdsMogoLayout(this, "7f8bfa79a54a42018c1b195dfbb56a1c",true);
//		lly_center.addView(lly_center);
		
		/////////////////////////////////////////////////////////////////
		
		
		//aml.setAdsMogoListener(this);
		
		mSeekBar = (SeekBar)findViewById(R.id.movies_seekbar);
		mSeekShow = (TextView)findViewById(R.id.mSeekShow);
		
		mSeekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			//拖动完成事件
			@Override
			public void onStopTrackingTouch(SeekBar sb) {
				// TODO Auto-generated method stub
				mViewPager.setCurrentItem(sb.getProgress());
				
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				// TODO Auto-generated method stub
				//showpagelable(arg1);
				tv_pageshow.setText((arg1)+"/"+(pList.size()));
				mSeekShow.setText((arg1)+"/"+(pList.size()));
			}
		});
		
		//注册更新广播
		IntentFilter intentFilter = new IntentFilter(Tag.OMUPDATEUI);
		registerReceiver(onMeasureBroadcastReceiver , intentFilter);
		
		
		
		//获取历史
		//getHistory();
		
		spAdapter = new AdSamplePagerAdapter(mContext,pList,adinfo);

		tv_mmIntro = (ScrollTextView)findViewById(R.id.movie_intro);
		tv_numline = (TextView)findViewById(R.id.tv_numline);
		mMovieIntro = (TextView)findViewById(R.id.mMovieIntro);
		
		mMovieIntro.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				++curLine;
				int numline = curLine%numlines;
				mMovieIntro.setText(tv_mmIntro.getLineString(numline));
				tv_numline.setText((numline+1)+"/"+numlines);		
				
			}
		});
		
		mViewPager.setAdapter(spAdapter);


		
		
		
		btn_back = (Button)findViewById(R.id.btn_movieback);
		btn_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		
		tv_moviename = (TextView)findViewById(R.id.movie_name);
		tv_moviename.setText(adinfo.getName());
		
		
		//tv_mIntro = (TextView)findViewById(R.id.tv_mintro);
		
//		int w = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); 
//	    int h = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED); 
//	    tv_mIntro.measure(w, h); 
//	    int height =tv_mIntro.getMeasuredHeight();
//	    int width =tv_mIntro.getMeasuredWidth();
//	    
//	    Logger.error("width:"+width);
	    
	    
//	    ViewTreeObserver vto = tv_mIntro.getViewTreeObserver(); 
//	    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
//	        public boolean onPreDraw() { 
//	            //int height = tv_mIntro.getMeasuredHeight(); 	            
//	            if(mTvWidth==0)
//	            {
//	            	mTvWidth = tv_mIntro.getMeasuredWidth(); 
//	            } 
//	            return true; 
//	        } 
//	    }); 
	    
	    
		showpagelable(0);
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub
				//Logger.info("onPageSelected:"+position);
				curPos = position;	
				numlines = 0;	
			    curLine = 0;
				
				showpagelable(position);
				
				if(lly_head.getVisibility()!=View.GONE)
				{
					hidelly();
				}
				
				if(curPos==pList.size()-1)
				{
					lly_left.setVisibility(View.GONE);
					lly_right.setVisibility(View.GONE);
				}else{
					lly_left.setVisibility(View.VISIBLE);
					lly_right.setVisibility(View.VISIBLE);
				}
				
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		//阅读全部
		btn_readall = (Button)findViewById(R.id.btn_readall);
		btn_readall.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				
				++curLine;
				int numline = curLine%numlines;
				mMovieIntro.setText(tv_mmIntro.getLineString(numline));
				tv_numline.setText((numline+1)+"/"+numlines);	
				
				
			}
		});
		
		
		//2013-8-19 modify
        lly_left = (LinearLayout)findViewById(R.id.lly_left);
        lly_left.setOnClickListener(new OnClickListener() {
			//上一页
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(curPos-1>=0&&curPos!=pList.size()-1)
				{
					curPos = curPos-1;
					mViewPager.setCurrentItem(curPos,true);
				}
			}
		});
        
        
        lly_right = (LinearLayout)findViewById(R.id.lly_right);
        lly_right.setOnClickListener(new OnClickListener() {
			//下一页
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(curPos+1<pList.size())
				{
					curPos = curPos+1;
					mViewPager.setCurrentItem(curPos,true);
				}
			}
		});
		
	}
	
	
	
	
	
	public void touchview()
	{
		if(lly_bottom.getVisibility()==View.GONE)
		{
			showlly();
		}else{
			hidelly();
		}
	}
	
	
	
	
	
	public void showlly()
	{
		lly_botmintro.startAnimation(animation);
		lly_botmintro.setVisibility(View.GONE);
		
		lly_bottom.startAnimation(animation1); 
		lly_head.startAnimation(head_in); 
		lly_bottom.setVisibility(View.VISIBLE);
		lly_head.setVisibility(View.VISIBLE);
	}
	
	public void hidelly()
	{
		lly_botmintro.startAnimation(animation1);
		lly_botmintro.setVisibility(View.VISIBLE);
		
		lly_bottom.startAnimation(animation); 
		lly_head.startAnimation(head_out); 
		lly_bottom.setVisibility(View.GONE);
		lly_head.setVisibility(View.GONE);
	}
	
	
	
	
	
	
	
	
	public void showpagelable(int position)
	{
		
		
		curPage = position;
		tv_pageshow.setText((curPage+1)+"/"+(pList.size()));
		//Logger.error("TEXT:"+pList.get(position).getIntro());
		if(curPage!=pList.size()-1)
		{
			Logger.error("TEXT:"+pList.get(position).getIntro());
			tv_mmIntro.setScrollText(pList.get(position).getIntro());
			lly_botmintro.setVisibility(View.VISIBLE);
		}else{
			lly_botmintro.setVisibility(View.GONE);
		}
		
		
		//tv_mmIntro.getline
		
		if(mSeekBar!=null&&pList!=null)
		{
			mSeekBar.setMax(pList.size());
			mSeekBar.setProgress((curPage+1));
			mSeekShow.setText((curPage+1)+"/"+(pList.size()));
		}				

	}
	
	private BroadcastReceiver onMeasureBroadcastReceiver = new BroadcastReceiver() 
	{
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			if(numlines==0)
			{
				numlines = tv_mmIntro.getSize();
				if(numlines<=1)
				{
					mMovieIntro.setText(tv_mmIntro.getScrollText());					
				}else{									
					mMovieIntro.setText(tv_mmIntro.getLineString(curLine));
				}
				tv_numline.setText((curLine+1)+"/"+numlines);
				
			}
			
		}
		
	};

	

	@Override
	public void onConfigurationChanged(Configuration newConfig)
	{
	    super.onConfigurationChanged(newConfig);
	     
	    if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE)
	    {
	        //Logger.info("ORIENTATION_LANDSCAPE");
	    }
	    if(this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
	    {
	    	//Logger.info("ORIENTATION_PORTRAIT");
	    }
	    if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO)
	    {
	    	
	    }
	    else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES)
	    {
	    	
	    }
	}


	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		mContext.unregisterReceiver(onMeasureBroadcastReceiver);
		super.onDestroy();
	}

	//记录书签
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		
		super.onPause();
	}


	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		
		super.onResume();
	}


	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		
		super.onStop();
	}
	


	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			finish();
			break;
		}
		return super.onKeyDown(keyCode, event);
	}





	

}
